﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Coevery.AppFramework.MetadataServiceReference;
using Coevery.AppFramework.WinForms.MVC;
using Coevery.AppFramework.WinForms.Views;
using Coevery.AppFramework.WinForms.ViewInterface;
using System.IO;

using Coevery.Domain.Impl;

namespace Coevery.Win.DocumentModule.Document
{

    public partial class DocumentDetailForm : XtraForm, IDocumentDetailForm, IView
    {
        public event EventHandler Saved;
        public void OnSaved(EventArgs e)
        {
            EventHandler handler = Saved;
            if (handler != null) handler(this, e);
        }


        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            this.Dispose();
        }

        public DocumentDetailForm()
        {
            InitializeComponent();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            OnSaved(e);
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public bool ValidateData()
        {
            var result = dxValidationProvider.Validate();
            var firstInvalideControl = dxValidationProvider.GetInvalidControls().FirstOrDefault();
            if (firstInvalideControl != null)
            {
                firstInvalideControl.Focus();
            }
            return result;
        }

        public void InitEditors(Entity entity, Coevery.AppFramework.WinForms.Messages.EntityDetailWorkingMode _workingMode)
        {
        }

        public void InitHOptionSets(ReadOnlyCollection<OptionSet> optionsetlist)
        {
        }

        public void Bind(object entity)
        {
            DocumentBindingSource.DataSource = entity;
        }

        public void PostEditors()
        {
        }

        public bool ShowCheckOptions
        {
            set
            {
                PutDocumentChEdit.Visible = value;
                PutDocumentDateToModifyEdit.Visible = value;
            }
        }

        public bool UpdateLatestFeadback
        {
            get { return PutDocumentDateToModifyEdit.Checked; }
        }

        public bool AppandDescription
        {
            get { return PutDocumentChEdit.Checked; }
        }

        public void SetEditorStatus(bool isReadOnly)
        {
        }

        public void BeginInit()
        {
        }

        public void EndInit()
        {
        }

        public void SetDescriptionVisible(bool isVisible)
        {
            PutDocumentChEdit.Visible = isVisible;
        }

        public void SetModifyEditVisible(bool isVisible)
        {
            PutDocumentDateToModifyEdit.Visible = isVisible;
        }

        public string EntityName
        {
            get;
            set;
        }

        public event EventHandler ObjectChanged;

        public DevExpress.XtraBars.Ribbon.RibbonControl GetRibbon()
        {
            return null;
        }

        public IControllerManager ControllerManager
        {
            get;
            set;
        }

        public bool IsChildForm
        {
            get
            {
                return false;
            }
            set
            {
            }
        }

        public object Model
        {
            get;
            set;
        }

        public string ViewName
        {
            get;
            set;
        }

        public void BindDataToView()
        {
            
        }

        public void ShowView()
        {

        }

        public void HideView()
        {
            
        }


        public void ActiveView()
        {
            
        }

        public void CloseView()
        {
            Close();
        }

        public string ViewType
        {
            get;
            set;
        }


        public Guid WorkSpaceID
        {
            get;
            set;
        }

        private void DocumentDetailForm_Load(object sender, EventArgs e)
        {

        }


        public AppFramework.WinForms.Context.IWorkSpaceContext Context
        {
            get;
            set;
        }

        private void DocumentDetailForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void documentBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void SubjectTextEdit_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ICSharpCode.Core.Services.ServiceManager.Instance = new Coevery.AppFramework.WinForms.Services.ServiceManager();
            ICSharpCode.Core.Services.ServiceManager.Instance.LoggingService.Debug("Se llama a openFileDialog1_FileOk");
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
                  
            int size = -1;
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;
                try
                {
                    string text = File.ReadAllText(file);                
                    size = text.Length;

                    byte[] imageBytes = File.ReadAllBytes(file);
                    Coevery.Domain.Impl.Document doc = DocumentBindingSource.Current as Coevery.Domain.Impl.Document;
                    doc.BinaryData = imageBytes;
              
                }
                catch (IOException)
                {
                }
            }
            Console.WriteLine(size); // <-- Shows file size in debugging mode.
            Console.WriteLine(result); // <-- For debugging use.
        }

        private void openFileDialog1_FileOk_1(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }
    }
}
